import { createRouter, createWebHistory } from "vue-router";
const routes = [
    {
        path: '/Login',
        name: 'Login',
        // 异步加载，打包时代码分割，性能优化
        component: () => import('../components/Login.vue')
    },
    {
        path: '/',
        redirect: 'Login'
    },
    {
        path: '/Main',
        name: 'Main',
        component: () => import('../components/Main.vue')
    }
]

const router = createRouter({
    history: createWebHistory(), // 路由模式：history模式
    routes: routes
})

// 配置全局前置守卫
router.beforeEach((to, from, next) => {
    // has token
    console.log(sessionStorage.getItem("token"));
    if(sessionStorage.getItem("token")){
        console.log("1111");
        next();//放行
    }else if(to.path === "/Login"){
 
     next();//放行
    }else{
        alert("请填写正确的页面！！！")
       next({ path: '/Login' });
    }
    
    //next(); // 必须调用该方法来resolve这个钩子
});

export default router;